﻿using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using CookingBg.Services.Proxies;

namespace CookingBg.Services
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class UsefullService
    {
        #region Products

        [WebGet(UriTemplate = "GetProductsCount", ResponseFormat = WebMessageFormat.Json)]
        public int GetProductsCount()
        {
            var model = new Entities();
            return model.Products.Count();
        }

        [WebGet(UriTemplate = "GetProducts?startIndex={startIndex}&count={count}&sorting={sorting}", ResponseFormat = WebMessageFormat.Json)]
        public IEnumerable<SmallListProxy> GetProducts(int startIndex, int count, int sorting)
        {
            var model = new Entities();
            var usefulls = from u in model.Products
                           orderby u.Title
                           select new SmallListProxy
                                      {
                                          ID = u.ID,
                                          Title = u.Title,
                                          SmallImageUrl = u.SmallImageUrl
                                      };
            return usefulls.Skip(startIndex).Take(count);
        }

        [WebGet(UriTemplate = "GetProduct?id={id}", ResponseFormat = WebMessageFormat.Json)]
        public BigUsefull GetProduct(int id)
        {
            var model = new Entities();
            var userfulls = from u in model.Products
                            where u.ID == id
                            select new BigUsefull
                            {
                                ID = u.ID,
                                Title = u.Title,
                                Description = u.Description,
                                ImageUrl = u.ImageUrl
                            };
            return userfulls.FirstOrDefault();
        }

        [WebGet(UriTemplate = "SearchProducts?search={search}", ResponseFormat = WebMessageFormat.Json)]
        public IEnumerable<SmallListProxy> SearchProducts(string search)
        {
            var model = new Entities();
            search = search.ToUpper();
            var usefulls = from u in model.Products
                           where u.Title.ToUpper().Contains(search)
                           orderby u.Title
                           select new SmallListProxy
                           {
                               ID = u.ID,
                               Title = u.Title,
                               SmallImageUrl = u.SmallImageUrl
                           };
            return usefulls;
        }

        [WebGet(UriTemplate = "SearchProductAndSpices?search={search}", ResponseFormat = WebMessageFormat.Json)]
        public IEnumerable<SmallListProxy> SearchProductAndSpices(string search)
        {
            var model = new Entities();
            search = search.ToUpper();
            var products = from u in model.Products
                           where u.Title.ToUpper().StartsWith(search)
                           orderby u.Title
                           select new SmallListProxy
                           {
                               ID = u.ID,
                               Title = u.Title,
                               SmallImageUrl = u.SmallImageUrl
                           };
            var spices = from u in model.Spices
                           where u.Title.ToUpper().StartsWith(search)
                           orderby u.Title
                           select new SmallListProxy
                           {
                               ID = u.ID,
                               Title = u.Title,
                               SmallImageUrl = u.SmallImageUrl
                           };
            return products.Union(spices);
        }

        #endregion

        #region Spices

        [WebGet(UriTemplate = "GetSpicesCount", ResponseFormat = WebMessageFormat.Json)]
        public int GetSpicesCount()
        {
            var model = new Entities();
            return model.Spices.Count();
        }

        [WebGet(UriTemplate = "GetSpices?startIndex={startIndex}&count={count}&sorting={sorting}", ResponseFormat = WebMessageFormat.Json)]
        public IEnumerable<SmallListProxy> GetSpices(int startIndex, int count, int sorting)
        {
            var model = new Entities();
            var usefulls = from u in model.Spices
                           orderby u.Title
                           select new SmallListProxy
                           {
                               ID = u.ID,
                               Title = u.Title,
                               SmallImageUrl = u.SmallImageUrl
                           };
            return usefulls.Skip(startIndex).Take(count);
        }

        [WebGet(UriTemplate = "GetSpice?id={id}", ResponseFormat = WebMessageFormat.Json)]
        public BigUsefull GetSpice(int id)
        {
            var model = new Entities();
            var userfulls = from u in model.Spices
                            where u.ID == id
                            select new BigUsefull
                            {
                                ID = u.ID,
                                Title = u.Title,
                                Description = u.Description,
                                ImageUrl = u.ImageUrl
                            };
            return userfulls.FirstOrDefault();
        }

        [WebGet(UriTemplate = "SearchSpices?search={search}", ResponseFormat = WebMessageFormat.Json)]
        public IEnumerable<SmallListProxy> SearchSpices(string search)
        {
            var model = new Entities();
            search = search.ToUpper();
            var usefulls = from u in model.Spices
                          where u.Title.ToUpper().Contains(search)
                          orderby u.Title
                          select new SmallListProxy
                          {
                              ID = u.ID,
                              Title = u.Title,
                              SmallImageUrl = u.SmallImageUrl
                          };
            return usefulls;
        }

        #endregion

        #region Advices

        [WebGet(UriTemplate = "GetAdvicesCount", ResponseFormat = WebMessageFormat.Json)]
        public int GetAdvicesCount()
        {
            var model = new Entities();
            return model.Advices.Count();
        }

        [WebGet(UriTemplate = "GetAdvices?startIndex={startIndex}&count={count}&sorting={sorting}", ResponseFormat = WebMessageFormat.Json)]
        public IEnumerable<SmallListProxy> GetAdvices(int startIndex, int count, int sorting)
        {
            var model = new Entities();
            var usefulls = from u in model.Advices
                           orderby u.Title
                           select new SmallListProxy
                           {
                               ID = u.ID,
                               Title = u.Title,
                               SmallImageUrl = u.SmallImageUrl
                           };
            return usefulls.Skip(startIndex).Take(count);
        }

        [WebGet(UriTemplate = "GetAdvice?id={id}", ResponseFormat = WebMessageFormat.Json)]
        public BigUsefull GetAdvice(int id)
        {
            var model = new Entities();
            var userfulls = from u in model.Advices
                            where u.ID == id
                            select new BigUsefull
                            {
                                ID = u.ID,
                                Title = u.Title,
                                Description = u.Description,
                                ImageUrl = u.ImageUrl
                            };
            return userfulls.FirstOrDefault();
        }

        [WebGet(UriTemplate = "SearchAdvices?search={search}", ResponseFormat = WebMessageFormat.Json)]
        public IEnumerable<SmallListProxy> SearchAdvices(string search)
        {
            var model = new Entities();
            search = search.ToUpper();
            var usefulls = from u in model.Advices
                          where u.Title.ToUpper().Contains(search)
                          orderby u.Title
                          select new SmallListProxy
                          {
                              ID = u.ID,
                              Title = u.Title,
                              SmallImageUrl = u.SmallImageUrl
                          };
            return usefulls;
        }

        #endregion
    }
}